// groupnpc.txt
// Exactly like basicnpc, with one difference. When it first spots a foe, sends
// an alert message to everyone in its group. Good for when you have a band of 
// monsters you don't want the party to be able to pick off one at a time.
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//   Cell 4 - Group creature is in.  (You don't need to set this, but it
//     makes life a bit easier.  Leave it at 0 to do nothing.)
//   Cell 5,6 - SDF to increment.
//   Cell 7 - Amount to increment by.

begincreaturescript;

variables;

short i,target;
short i_gave_alert = 0;
short in_fire,last_abil_time,which_atk,trgt,firedmg,mctx,mcty,rctx,rcty;
short redct,redctx,redcty,count;
short kill_u,im_done,bmessage;

body;

beginstate INIT_STATE;
	im_done = 0;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
	if (get_memory_cell(4) > 0)
		add_char_to_group(my_number(),get_memory_cell(4));
	break;

beginstate DEAD_STATE;

	kill_u = 0;
	while(kill_u <= 100){
		kill_char(1000,3,0);
		revive_party();
		kill_u = (kill_u + 1);
	}

break;

beginstate START_STATE; 

	if(tick_difference(last_abil_time,get_current_tick()) >= 1){
		im_done = (im_done + 1);
		if(im_done >= get_ran(1,1,3)){
			trgt = 86;
			while(trgt <= 119){
				if(char_ok(trgt) == TRUE)
					erase_char(trgt);

				trgt = (trgt + 1);
			}
			set_incidental_sound(1);
			set_total_visibility(1);
			force_view_center(17,10);
			force_instant_terrain_redraw();
			pause(2);

			activate_hidden_group(3);
			put_boom_on_space(17,6,2,0);
			force_instant_terrain_redraw();
			run_animation_sound(10);
			pause(2);

			set_character_facing(7,0);
			force_instant_terrain_redraw();
			pause(3);

			reset_dialog();
			add_dialog_str(0,"The wizard in red-- what is he doing here, again?",0);
			add_dialog_str(1,"_Profanus Pyre!  I need help downstairs immediately.  The portals have been opened, and troops are beginning to filter through._",0);
			add_dialog_str(2,"Portals?  Troops?  Oh no...  Is this base getting reinforcements?!",0);
			add_dialog_choice(0,"Damnit!");
			bmessage = run_dialog(1);

			set_character_pose(7,1);
			force_instant_terrain_redraw();
			pause(2);

			erase_char(7);
			rctx = 12;
			while(rctx <= 22){
				rcty = 10;
				while(rcty <= 16){
					set_floor(rctx,rcty,177);

					rcty = (rcty + 1);
				}

				rctx = (rctx + 1);
			} 
			put_boom_on_space(20,13,2,0);
			force_instant_terrain_redraw();
			run_animation_sound(10);
			pause(5);

			reset_dialog();
			if(party_size() == 1){
				add_dialog_str(0,"Man.  Who IS this guy?  He's a rakshasa, and yet he's not particularly bloodthirsty.  At least, I don't THINK he is.  Is he lying to me?  What's his deal?",0);
				add_dialog_str(1,"_Heh...  you again, I see.  You're a regular troublemaker.  I hope you forgive me for wasting your time at the docks...  But speaking of time, I don't have much, so I'll be brief._",0);
			}
			if(party_size() > 1){
				add_dialog_str(0,"Man.  Who IS this guy?  He's a rakshasa, and yet he's not particularly bloodthirsty.  At least, we don't THINK he is.  Is he lying to us?  What's his deal?",0);
				add_dialog_str(1,"_Heh...  you again, I see.  You're regular troublemakers.  I hope you forgive me for wasting your time at the docks...  But speaking of time, I don't have much, so I'll be brief._",0);
			}
			add_dialog_str(2,"_I want you to keep fighting.  You may be needed in the near future to avert a serious danger that threatens a great many things._",0);
			add_dialog_str(3,"_This danger, I am attempting to prevent...  if I fail, the task will fall onto you.  You'll know what I mean, and I hope you're capable of succeeding.  Now if you'll excuse me..._",0);
			add_dialog_choice(0,"Wait!  Who are you?");
			bmessage = run_dialog(1);

			set_character_pose(9,1);
			force_instant_terrain_redraw();
			pause(2);

			erase_char(9);
			put_boom_on_space(17,6,2,0);
			force_instant_terrain_redraw();
			run_animation_sound(10);
			pause(5);
			set_total_visibility(0);
			force_instant_terrain_redraw();

			reset_dialog();
			add_dialog_str(0,"Damnit!  Did he just leave me in these blasted barriers?  How am I supposed to get out?  Nevermind that-- Imperial soldiers are flowing into this fortress!",0);
			add_dialog_str(1,"_Hey!  Are you around here somewhere?  If you can hear me, please respond!_",20);
			add_dialog_str(2,"Hunter!  Is that you?",0);
			add_dialog_choice(0,"I'm over here!  Hunter!");
			bmessage = run_dialog(1);

			award_party_xp(500,25);
			award_party_xp(500,25);
			award_party_xp(500,25);

			set_flag(15,1,7);
			end();
		}

		mctx = char_loc_x(my_number());
		mcty = char_loc_y(my_number());
		print_str_color("Profanus Pyre casts Fireblast MkII.",1);
		set_character_pose(my_number(),1);
		force_instant_terrain_redraw();
		pause(2);
		play_sound(25);

		trgt = 0;
		while(trgt <= 119){
			if(((char_ok(trgt) == TRUE) && (char_attitude_to_char(my_number(),trgt) == 2)) && (can_see_char(trgt) == TRUE)){
				rctx = char_loc_x(trgt);
				rcty = char_loc_y(trgt);
				put_straight_zap(mctx,mcty,rctx, rcty,0);
				put_boom_on_space(rctx,rcty,0,0);
				put_effect_on_space(rctx,rcty,9,4, 2);
				run_animation_sound(51);
				firedmg = get_ran(1,100,200);
				if(rctx < mctx)
					redctx = (mctx - rctx);
				if(rctx > mctx)
					redctx = (rctx - mctx);
				redctx = (redctx - 1);
				if(rcty < mcty)
					redcty = (mcty - rcty);
				if(rcty > mcty)
					redcty = (rcty - mcty);
				redcty = (redcty - 1);
				redct = (redctx + redcty);
				redct = (redct * 5);
				firedmg = (firedmg - redct);

				damage_char(trgt,firedmg,1);
			}

			trgt = (trgt + 1);
		}
		set_character_pose(my_number(),0);
		force_instant_terrain_redraw();

		deduct_ap(8);
		last_abil_time = get_current_tick();
	}

break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;